from time import sleep

import pytest
from selenium import webdriver
from selenium.webdriver.edge.service import Service


@pytest.fixture(scope='module') # 整个.py文件都只执行一次
def driver():
    # 修复路径问题的三种方式（任选一种）

    # 方式1: 在字符串前加r表示原始字符串（推荐）
    # EDGE_DRIVER_PATH = r"C:\Users\86186\PycharmProjects\UIProject\MicrosoftWebDriver.exe"

    # 方式2: 使用双反斜杠
    EDGE_DRIVER_PATH = "C:\\Users\86186\\PycharmProjects\\ui_Auto\\MicrosoftWebDriver.exe"

    # 方式3: 使用正斜杠
    # EDGE_DRIVER_PATH = "C:/Users/86186/PycharmProjects/UIProject/MicrosoftWebDriver.exe"
    # 创建Service对象
    service = Service(executable_path=EDGE_DRIVER_PATH)

    # 配置Edge选项
    edge_options = webdriver.EdgeOptions()
    edge_options.add_experimental_option("excludeSwitches", ["enable-automation"])

    # 初始化Edge浏览器
    driver = webdriver.Edge(
        service=service,
        options=edge_options
    )
    driver.maximize_window()
    yield driver
    sleep(5)
    driver.quit()